package com.chenwc.qiniukodo;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.qiniu.storage.model.FetchRet;
import com.qiniu.storage.model.FileInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 七牛云对象存储测试类
 * @author chenwc
 */
public class KodoTest {

    private static final Logger logger = LoggerFactory.getLogger(KodoTest.class);

    @BeforeAll
    public static void getMinioClient() {
        KodoUtil.init("",
                "",
                "");
    }

    @Test
    @Tag("GetUploadToken")
    public void getUploadToken() {
        logger.info(KodoUtil.getUploadToken());
    }

    @Test
    @Tag("UploadFile")
    public void uploadFile() {
        File file = new File("D:\\Download\\code-traceid：f07e361e3e998202.txt");
        String key = file.getName();
        KodoUtil.uploadFile(file.getAbsolutePath(), key);
    }

    @Test
    @Tag("UploadFileByBytes")
    public void uploadFileByBytes() {
        byte[] uploadBytes = "hello world".getBytes(StandardCharsets.UTF_8);
        KodoUtil.uploadFileByBytes(uploadBytes, "hello.txt");
    }

    @Test
    @Tag("ResumeUploadFromBreakpoint")
    public void resumeUploadFromBreakpoint() {
        String filePath = "D:\\Download\\jdk-8u401-windows-x64.exe";
        String key = "jdk-8u401-windows-x64.exe";
        KodoUtil.resumeUploadFromBreakpoint(filePath, key);
    }


    @Test
    @Tag("DownloadPublicZoneFileBySdk")
    public void downloadPublicZoneFileBySdk() {
        String domainOfBucket = "http://.hn-bkt.clouddn.com";
        String key = "code-traceid：f07e361e3e998202.txt";
        String attname = "code-traceid：f07e361e3e998202.txt";
        String url = KodoUtil.downloadPublicZoneFileBySdk(domainOfBucket, key, attname);
        logger.info(url);
    }

    @Test
    @Tag("DownloadPublicZoneFileByUrl")
    public void downloadPublicZoneFileByUrl() {
        String domainOfBucket = "http://.hn-bkt.clouddn.com";
        String key = "code-traceid：f07e361e3e998202.txt";
        String url = KodoUtil.downloadPublicZoneFileByUrl(domainOfBucket, key);
        logger.info(url);
    }

    @Test
    @Tag("DownloadPrivateZoneFileByUrl")
    public void downloadPrivateZoneFileByUrl() {
        String domainOfBucket = "http://.hn-bkt.clouddn.com";
        String key = "code-traceid：f07e361e3e998202.txt";
        long expireInSeconds = 3600;
        String url = KodoUtil.downloadPrivateZoneFileByUrl(domainOfBucket, key, expireInSeconds);
        logger.info(url);
    }

    @Test
    @Tag("DownloadPrivateZoneFileBySdk")
    public void downloadPrivateZoneFileBySdk() {
        String domainOfBucket = "http://.hn-bkt.clouddn.com";
        String key = "code-traceid：f07e361e3e998202.txt";
        String attname = "code-traceid：f07e361e3e998202.txt";
        String url = KodoUtil.downloadPrivateZoneFileBySdk(domainOfBucket, key, attname, 3600);
        logger.info(url);
    }

    @Test
    @Tag("GetFileInfo")
    public void getFileInfo() {
        String key = "code-traceid：f07e361e3e998202.txt";
        FileInfo fileInfo = KodoUtil.getFileInfo(key);
        logger.info(JSON.toJSONString(fileInfo, JSONWriter.Feature.WriteMapNullValue));
    }

    @Test
    @Tag("DeleteFile")
    public void deleteFile() {
        String key = "code-traceid：f07e361e3e998202.txt";
        KodoUtil.deleteFile(key);
    }

    @Test
    @Tag("ListFiles")
    public void listFiles() {
        String prefix = "";
        List<FileInfo> fileInfos = KodoUtil.listFiles(prefix);
        logger.info(JSON.toJSONString(fileInfos, JSONWriter.Feature.WriteMapNullValue));
    }

    @Test
    @Tag("FetchRemoteFile")
    public void fetchRemoteFile() {
        String remoteSrcUrl = "https://www.baidu.com/img/bd_logo1.png";
        String key = "bd_logo1.png";
        FetchRet fetchRet =KodoUtil.fetchRemoteFile(remoteSrcUrl, key);
        logger.info(JSON.toJSONString(fetchRet, JSONWriter.Feature.WriteMapNullValue));
    }
}
